// miniprogram/pages/home_center/device_manage/index.js.js

import { getDeviceInfo, removeDevice } from '@ray-js/wechat';

Page({
  /**
   * 页面的初始数据
   */
  data: {
    dialogShow: false,
    devInfo: {},
    device_id: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const { device_id } = options;
    this.setData({ device_id });
    getDeviceInfo({ deviceId: device_id }).then((res) => {
      this.setData({ devInfo: res });
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  showDeviceInfo: function () {
    this.setData({ dialogShow: true });
  },
  removeDevice: async function () {
    const { device_id } = this.data;
    try {
      await removeDevice({ deviceId: device_id });
      wx.navigateBack({ delta: 2 });
    } catch (error){
      return wx.showToast({
        title: '移除失败',
        icon: 'error',
      });
    }
  },
});
